package day1.homework;
//冒泡排序
import java.util.Arrays;

public class MaoPao {
    public static void main(String[] args) {
        int[] arr = {5,2,8,1,6};
        System.out.println("排序前："+ Arrays.toString(arr));
        bubbleSort(arr);
    }
    public static void bubbleSort(int[] arr){
        int n = arr.length;
        for (int i = 0; i < n-1; i++) {
            //设置判断排序结束值
            boolean flag = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
        System.out.println("排序后:"+Arrays.toString(arr));
    }
}
